﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Group16.Class_Library;

namespace Group16.Controller
{
    public partial class IncomeStatement : SearchInvoice
    {
        private  DateTime _startDate;
        private DateTime _endDate;
        private int _grossIncome =0;
        public IncomeStatement()
        {
            InitializeComponent();
            Setting();
        }
        public DateTime StartDate
        {
            get { return _startDate; }
            set { _startDate = value; }
        }
        public DateTime EndDate
        {
            get { return _endDate; }
            set { _endDate = value; }
        }
        public int GrossIncome
        {
            get { return _grossIncome; }
            set { _grossIncome = value; }
        }
        private void Setting()
        {
            dtpStart.MinDate = GetDate(false);
            dtpStart.MaxDate = GetDate(true);
            dtpEnd.MinDate = GetDate(false);
            dtpEnd.MaxDate = GetDate(true);
        }
        private DateTime GetDate(bool isMax)
        {
            string com;
            if (isMax)
                com = "max";
            else
                com = "min";
            try
            {
                string comText = "Select " + com + "(DateMade) from Invoice";
                Object obj = DataBaseInteraction.ExecuteCommand(comText, 3);
                if ( obj != null)
                    return (DateTime)obj;
                else
                    return DateTime.Now;
            }
            catch (SqlException se)
            {
                MessageBox.Show("Unable to get mindate from database " + se);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
            return DateTime.Now;
        }
        protected override void Search()
        {
            StartDate = dtpStart.Value;
            EndDate = dtpEnd.Value;
            if (StartDate > EndDate)
            {
                lbMessage.Text = "Start date cannot be larger than end date!";
                return;
            }
            Manager = new InvoiceManager();
            Manager.GetInvoice(StartDate, EndDate);
            gridDetail.DataSource = Manager.Invoices;
        }

        private void btnMake_Click(object sender, EventArgs e)
        {
            if (Manager == null || Manager.Invoices.Count <= 0 || gridDetail.RowCount == 0)
                return;
            StartDate = dtpStart.Value;
            EndDate = dtpEnd.Value;
            using (StatisticForm sf = new StatisticForm(StartDate, EndDate))
            {
                sf.ShowDialog();
            }
            ClearData();
        }
    }
}
